home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / CONSTRAI / OP2_IMPL.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  15.6 KB  |  423 lines

  1.  
  2. package sub_arctic.constraints;
  3.  
  4. import sub_arctic.lib.interactor;
  5. import sub_arctic.lib.interactor_consts;
  6. import sub_arctic.lib.manager;
  7. import sub_arctic.lib.sub_arctic_error;
  8.  
  9. import java.util.Vector;
  10.  
  11. /** 
  12.  * Constraint implementation class to provide encoding for standard 2 operand 
  13.  * lightweight constraints.  This object takes 2 std_objpart_encoding objects 
  14.  * representing operands, along with a signed 15 bit value to provide a 
  15.  * constant to the function.<p>
  16.  *
  17.  * 2 operand constraints are encoded as:
  18.  * <pre>
  19.  *            15          6      6     5
  20.  *     KKKKKKKKKKKKKKK AAAAAA BBBBBB 00010   30 2 operand operations 
  21.  *                              ...            [K is 15 bit signed]
  22.  *     KKKKKKKKKKKKKKK AAAAAA BBBBBB 11111   
  23.  * </pre>
  24.  * where KKK represents a 15 bit signed constant, AAA, and BBB represent 
  25.  * parameter objects, and XXX represents bits used to encode an op code.<p>
  26.  *
  27.  * @version $Id: op2_impl.java,v 1.8 1996/10/03 19:43:09 hudson Exp $
  28.  * @author Scott Hudson
  29.  */
  30.  
  31. public class op2_impl implements std_encoding_consts {
  32.  
  33.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  34.  
  35.   /** 
  36.    * Create a standard constraint given an op_code, 2 obj/part designators, 
  37.    * and a constant.
  38.    * 
  39.    * @param int                  op_code op code value for this operation.
  40.    * @param std_objpart_encoding op1     designator for operand 1
  41.    * @param std_objpart_encoding op2     designator for operand 2
  42.    * @param short                K       value for 15 bit signed constant
  43.    */
  44.   public static std_constraint create(
  45.     int op_code, std_objpart_encoding op1, std_objpart_encoding op2, short K) 
  46. {
  47.       return new std_constraint(encode(op_code,op1, op2, K), 
  48.                                   op1.orientation() | op2.orientation());
  49.     }
  50.  
  51.    //had:
  52.    //* @exception bad_constraint if the op code is out of range.
  53.  
  54.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  55.  
  56.   /** 
  57.    * Do an encoding given an op_code, 2 obj/part designators, and a constant.
  58.    * 
  59.    * @param int                  op_code op code value for this operation.
  60.    * @param std_objpart_encoding op1     designator for operand 1
  61.    * @param std_objpart_encoding op2     designator for operand 2
  62.    * @param short                K       value for 15 bit signed constant
  63.    */
  64.   public static int encode(
  65.     int op_code, std_objpart_encoding op1, std_objpart_encoding op2, short K) 
  66. {
  67.       if (op_code < OP2_MIN || op_code > OP2_MAX)
  68.     throw new sub_arctic_error(
  69.      "Unrecognized op code (" + op_code + ") used for 2 op constraint");
  70.  
  71.       return (((int)K)<<17) | ((op1.encoding() & OBJPART_MASK) << 11) | 
  72.                               ((op2.encoding() & OBJPART_MASK) << 5) | 
  73.                           ((op_code<<OP2_SHIFT) & OP2_MASK)  | OP2_LOBITS;
  74.     }
  75.  
  76.    //had:
  77.    //* @exception bad_constraint if the op code is out of range.
  78.  
  79.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  80.  
  81.   /** 
  82.    * Decode op code from given encoding.  This assumes we already know this
  83.    * is a 2-op encoding -- otherwise we get the wrong answer.<p>
  84.    * 
  85.    * @param int enc the encoding for a constraint.
  86.    */
  87.   public static int op_code(int enc) { return (enc & OP2_MASK) >> OP2_SHIFT; }
  88.  
  89.   /** 
  90.    * Decode first operand from given encoding.  This assumes we already know 
  91.    * this is a 2-op encoding -- otherwise we get the wrong answer.<p>
  92.    * 
  93.    * @param int enc the encoding for a constraint.
  94.    */
  95.   public static int op1(int enc) { return (enc >> 11) & OBJPART_MASK; }
  96.  
  97.   /** 
  98.    * Decode second operand from given encoding.  This assumes we already know 
  99.    * this is a 2-op encoding -- otherwise we get the wrong answer.<p>
  100.    * 
  101.    * @param int enc the encoding for a constraint.
  102.    */
  103.   public static int op2(int enc) { return (enc >> 5) & OBJPART_MASK; }
  104.  
  105.   /** 
  106.    * Decode constant from the given encoding.  This assumes we already know 
  107.    * this is a 2-op encoding -- otherwise we get the wrong answer.<p>
  108.    * 
  109.    * @param int enc the encoding for a constraint.
  110.    */
  111.   public static short const_val(int enc) 
  112.     { 
  113.       /* get the value out of the high 15 bits, sign extending with >> */
  114.       return (short)(enc>>17);
  115.     }
  116.  
  117.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  118.  
  119.   /* Part constants copied over from interactor_consts for convenience. */
  120.   public static final int X = interactor_consts.X;
  121.   public static final int Y = interactor_consts.Y;
  122.   public static final int W = interactor_consts.W;
  123.   public static final int H = interactor_consts.H;
  124.   public static final int VISIBLE = interactor_consts.VISIBLE;
  125.   public static final int ENABLED = interactor_consts.ENABLED;
  126.   public static final int PART_A  = interactor_consts.PART_A;
  127.   public static final int PART_B  = interactor_consts.PART_B;
  128.  
  129.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  130.  
  131.   /**
  132.    * Evaluate an encoded constraint function given its operand and constant
  133.    * values.  We assume here that it is has already been determined that
  134.    * the constraint is a 2-op constraint.<p>
  135.    *
  136.    * @param int        enc         the encoding from the constraint
  137.    * @param interactor constr_obj  the object being constrained
  138.    * @param int        val1        value of first operand
  139.    * @param int        val2        value of second operand
  140.    * @param int        cnst_val    value of constant parameter
  141.    * @param int        orient      orientation of the constraint (should be 
  142.    *                               HORIZONTAL or VERTICAL).
  143.    * @return int the result of the evaluation
  144.    */
  145.   public static int eval_fun(
  146.     int enc,
  147.     interactor constr_obj,
  148.     int        val1,
  149.     int        val2,
  150.     int        cnst_val, 
  151.     int        orient) 
  152. {
  153.       interactor        par;
  154.  
  155.       /* sanity check */
  156.       if (constr_obj == null) 
  157.     throw new sub_arctic_error("Null constrained object passed to " + 
  158.                 "op2_impl.eval_fun()");
  159.  
  160.       /* execute code for the encoded function */
  161.       switch(op_code(enc))
  162.     {
  163.           case OP_add:        return val1 + val2 + cnst_val;
  164.           case OP_subtract:   return val1 - val2 + cnst_val;
  165.           case OP_mult:       return val1 * val2 + cnst_val;
  166.           case OP_div:        if (val2 != 0) 
  167.                     return val1 / val2 + cnst_val; 
  168.                   else 
  169.                     return 0;
  170.           case OP_mod:       if (val2 != 0) 
  171.                    return val1 % val2 + cnst_val; 
  172.                   else 
  173.                     return 0;
  174.           case OP_and:        return (val1 & val2) & (cnst_val | 0xffff8000);
  175.           case OP_or:         return (val1 | val2) & (cnst_val | 0xffff8000);
  176.           case OP_xor:        return (val1 ^ val2) & (cnst_val | 0xffff8000);
  177.           case OP_min:        return ((val1 < val2) ? val1 : val2) + cnst_val;
  178.           case OP_max:        return ((val1 > val2) ? val1 : val2) + cnst_val;
  179.           case OP_ave:        return ((val1 + val2)/2) + cnst_val;
  180.           case OP_rotx:       return 0; //xx later
  181.           case OP_roty:       return 0; //xx later
  182.           case OP_if_enabled: if (constr_obj.enabled()) 
  183.                 return val1; 
  184.                   else 
  185.                 return val2;
  186.           case OP_if_visible: if (constr_obj.visible()) 
  187.                 return val1; 
  188.                   else 
  189.                 return val2;
  190.  
  191.           case OP_self_fun2:
  192.         /* use the object's custom_fun2() to compute the value */
  193.         return constr_obj.custom_fun2(val1, val2, cnst_val);
  194.  
  195.           case OP_parent_fun2:
  196.         /* get the parent and call its custom_fun2() */
  197.         par = constr_obj.parent();
  198.         if (par != null)
  199.           return par.custom_fun2(val1, val2, cnst_val);
  200.         else
  201.           return 0; 
  202.  
  203.       case OP_fill: return val2 - val1 - cnst_val;
  204.  
  205.       default:
  206.         /* something is wrong if we get here */
  207.         throw new sub_arctic_error("Improperly encoded constraint found in "+
  208.                      "op1_impl.eval_fun()");
  209.     }
  210.     }
  211.  
  212.    //had:
  213.    //* @exception bad_value if the encoding, or constrained object/part are 
  214.    //*                      malformed
  215.  
  216.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  217.  
  218.    /** 
  219.     * Evaluate an encoded constraint applied to the given part of the given
  220.     * object.  We assume here that it it has already been determined that
  221.     * the constraint is a 2-op constraint.<p>
  222.     *
  223.     * @param int        enc         the encoding from the constraint
  224.     * @param interactor constr_obj  the object being constrained
  225.     * @param int        constr_part the part being constrained
  226.     * @param int        orient      the orientation of the constraint
  227.     * @return int the result of the evaluation
  228.     */
  229.    public static int eval(
  230.      int        enc, 
  231.      interactor constr_obj, 
  232.      int        constr_part,
  233.      int        orient) 
  234. {
  235.        int               cnst_val, val1, val2; 
  236.        provider_part_ref ext;
  237.        Object            val;
  238.        interactor        par;
  239.  
  240.        /* sanity check */
  241.        if (constr_obj == null) 
  242.      throw new sub_arctic_error("Null constrained object passed to " + 
  243.                  "op2_impl.eval()");
  244.  
  245.        /* extract the constant */
  246.        cnst_val = const_val(enc);
  247.  
  248.        /* get the value for the operands */
  249.        val1 = std_constraint_impl.the_impl().fetch_value(
  250.                          op1(enc), constr_obj, orient);
  251.        val2 = std_constraint_impl.the_impl().fetch_value(
  252.                          op2(enc), constr_obj, orient);
  253.  
  254.        /* evaluate the encoded function */
  255.        return eval_fun(enc, constr_obj, val1, val2, cnst_val, orient);
  256.  
  257.      }
  258.  
  259.     //had:
  260.     //* @exception bad_value if the encoding, or constrained object/part are 
  261.     //*                      malformed
  262.  
  263.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  264.  
  265.    /** Test whether the given encoded constraint (constraining the given 
  266.     *  object) depends on the indicated object and part (expressed relative
  267.     *  to it).  Here we assume that the encoding is already known to contain
  268.     *  a 2-op constraint.<p>
  269.     *
  270.     * @param int        enc             The encoding for the constraint.
  271.     * @param interactor constr_obj      The object the constraint is attached to
  272.     * @param int        test_which_obj  The object we are asking about.  This 
  273.     *                                   can be one of the values OBJCODE_SELF, 
  274.     *                                   OBJCODE_PARENT, OBJCODE_SOME_CHILD, 
  275.     *                                   OBJCODE_PREV_SIBLING, or 
  276.     *                                   OBJCODE_NEXT_SIBLING.  
  277.     * @param int        test_which_part The part we are asking about.
  278.     * @param int        nth_child       for SOME_CHILD, this provides the index
  279.     *                    of the child (and is ignored otherwise).
  280.     * @param int        orient          Orientation of the constraint.  This 
  281.     *                                   should be HORIZONTAL or VERTICAL.
  282.     * @return boolean whether the given constraint depends upon the given value
  283.     */
  284.    public static boolean depends_on(
  285.      int        enc, 
  286.      interactor constr_obj,
  287.      int        which_obj, 
  288.      int        which_part, 
  289.      int        nth_child,
  290.      int        orient) 
  291. {
  292.        int op;
  293.  
  294.        /* extract op code to look for special case implicit operands */ 
  295.        op = op_code(enc);
  296.  
  297.        /* special cases here for if_enabled and if_visible */
  298.        if (op == OP_if_enabled && which_obj == OBJCODE_SELF)
  299.      {
  300.        // test for which_part == enabled here xx
  301.      }
  302.        else if (op == OP_if_visible && which_obj == OBJCODE_SELF)
  303.      {
  304.        // test for which_part == enabled here xx
  305.      }
  306.  
  307.        /* now have a look at the first operand dependency */
  308.        op = op1(enc);
  309.        if (std_constraint_impl.the_impl().part_depends_on(op, constr_obj, 
  310.          which_obj, which_part, nth_child, orient))
  311.      return true;
  312.  
  313.     /* if that didn't match try the second operand */
  314.     op = op2(enc);
  315.         return std_constraint_impl.the_impl().part_depends_on(op, constr_obj, 
  316.         which_obj, which_part, nth_child, orient);
  317.      }
  318.  
  319.     //had:
  320.     //* @exception bad_value if one of the parameters has an unrecognized value
  321.  
  322.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  323.  
  324.   /**
  325.    * Extract the set of objects/parts that a constraint depends on.  This
  326.    * produces a Vector with pairs of entries, the first being an interactor,
  327.    * and the second being an Integer which is the part number of that 
  328.    * interactor that is depended upon.<p>
  329.    *
  330.    * Here we assume that this is a 2 operand constraint.<p>
  331.    *
  332.    * @param int        enc        encoding value for the constraint in question.
  333.    * @param interactor constr_obj the object the constraint is attached to 
  334.    *                              (hence its referents are relative to).
  335.    * @param int        orient     Orientation of the constraint.  This 
  336.    *                              should be HORIZONTAL or VERTICAL.
  337.    * @return Vector containing pairs of objects, the first being an interactor
  338.    *                which is depended upon, and the second being an Integer
  339.    *                giving the part number of the part depended upon.
  340.    */
  341.   public static Vector mk_depend_list(int enc, interactor constr_obj,int orient) 
  342. {
  343.       int op;
  344.       Vector result;
  345.  
  346.       /* do the implicit dependencies first */
  347.       result = mk_implicit_depend_list(enc, constr_obj, orient);
  348.  
  349.       /* extract the first operand and process that */
  350.       op = op1(enc);
  351.       std_constraint_impl.the_impl().
  352.                           add_depend_obj_part(result, op, constr_obj, orient);
  353.  
  354.       /* and the second */
  355.       op = op2(enc);
  356.       std_constraint_impl.the_impl().
  357.                           add_depend_obj_part(result, op, constr_obj, orient);
  358.  
  359.       return result;
  360.     }
  361.  
  362.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  363.  
  364.   /**
  365.    * Extract the set of objects/parts that a particular constraint function 
  366.    * implicitly depends on.  This produces a Vector with pairs of entries, the 
  367.    * first being an interactor, and the second being an Integer which is the 
  368.    * part number of that interactor that is depended upon.<p>
  369.    *
  370.    * @param int        enc        encoding value for the constraint in question.
  371.    * @param interactor constr_obj the object the constraint is attached to 
  372.    *                              (hence its referents are relative to).
  373.    * @param int        orient     Orientation of the constraint.  This 
  374.    *                              should be HORIZONTAL or VERTICAL.
  375.    * @return Vector containing pairs of objects, the first being an interactor
  376.    *                which is depended upon, and the second being an Integer
  377.    *                giving the part number of the part depended upon.
  378.    */
  379.   public static Vector mk_implicit_depend_list(
  380.     int enc, interactor constr_obj,int orient)
  381.     {
  382.       int    op;
  383.       Vector result = new Vector(6);
  384.  
  385.       /* extract operand. */
  386.       op = op_code(enc);
  387.  
  388.       /* OP_if_enabled implicitly depends on self.enabled */
  389.       if (op == OP_if_enabled)
  390.     {
  391.       result.addElement(constr_obj);
  392.       result.addElement(new Integer(ENABLED));
  393.     }
  394.       /* OP_if_visible implicitly depends on self.visible */
  395.       else if (op == OP_if_visible)
  396.     {
  397.       result.addElement(constr_obj);
  398.       result.addElement(new Integer(VISIBLE));
  399.     }
  400.  
  401.       return result;
  402.     }
  403.  
  404.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  405. }
  406.  
  407. /*=========================== COPYRIGHT NOTICE ===========================
  408.  
  409. This file is part of the subArctic user interface toolkit.
  410.  
  411. Copyright (c) 1996 Scott Hudson and Ian Smith
  412. All rights reserved.
  413.  
  414. The subArctic system is freely available for most uses under the terms
  415. and conditions described in 
  416.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  417. and appearing in full in the lib/interactor.java source file.
  418.  
  419. The current release and additional information about this software can be 
  420. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  421.  
  422. ========================================================================*/
  423.